package cn.sdhuijin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sdhuijin.model.Branch;
import cn.sdhuijin.service.BranchService;

/**
 * @author sunp@sdhuijin.cn
 * @version 创建时间：2014年12月23日 下午4:49:34 类说明
 */
@Controller
@RequestMapping(value = { "/branch" })
public class BranchesController {

	@Inject
	private BranchService branchService;

	@ResponseBody
	@RequestMapping(value = { "/getBranchSelectList" })
	public String getBranchSelectList() {
		StringBuffer branchBuffer = new StringBuffer();
		List<Branch> branchList = new ArrayList<Branch>();
		branchList = branchService.getBranchSelectList();
		if (branchList.size() > 0) {
			for (int i = 0; i < branchList.size(); i++) {
				branchBuffer.append(branchList.get(i).getId());
				branchBuffer.append("&");
				branchBuffer.append(branchList.get(i).getName());
				branchBuffer.append(";");
			}
		}
		return branchBuffer.toString();
	}

	@ResponseBody
	@RequestMapping(value = { "/getChildrenBranchSelectList" })
	public String getChildrenBranchSelectList(
			@RequestParam("parentId") String parentId) {
		StringBuffer branchBuffer = new StringBuffer();
		List<Branch> branchList = new ArrayList<Branch>();
		Branch branch = branchService.searchOne(parentId);
		branchList = branchService.subordinateBranches(branch);
		if (branchList.size() > 0) {
			for (int i = 0; i < branchList.size(); i++) {
				branchBuffer.append(branchList.get(i).getId());
				branchBuffer.append("&");
				branchBuffer.append(branchList.get(i).getName());
				branchBuffer.append(";");
			}
		}
		return branchBuffer.toString();
	}
}
